{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For Colab only!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "try:\n",
    "  # %tensorflow_version only exists in Colab.\n",
    "  %tensorflow_version 2.x\n",
    "except Exception:\n",
    "  pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "cell_style": "split"
   },
   "outputs": [],
   "source": [
    "import tensorflow as tf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "cell_style": "split"
   },
   "outputs": [],
   "source": [
    "import torch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "cell_style": "split"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.0.0\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "print(tf.__version__)\n",
    "print(tf.test.is_gpu_available())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "cell_style": "split"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.4.0\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "print(torch.__version__)\n",
    "print(torch.cuda.is_available())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Scalar Vector and Tensor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "cell_style": "split"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "()\n",
      "0\n",
      "tf.Tensor(2.4, shape=(), dtype=float32)\n",
      "tf.Tensor([2.4], shape=(1,), dtype=float32)\n",
      "dim =  1\n",
      "tf.Tensor(\n",
      "[[1 2]\n",
      " [3 4]], shape=(2, 2), dtype=int32)\n",
      "dim =  2\n",
      "tf.Tensor(\n",
      "[[[1 2]\n",
      "  [3 4]]], shape=(1, 2, 2), dtype=int32)\n",
      "dim =  3\n",
      "tf.Tensor(\n",
      "[[[1]\n",
      "  [2]]\n",
      "\n",
      " [[3]\n",
      "  [4]]], shape=(2, 2, 1), dtype=int32)\n",
      "dim =  3\n"
     ]
    }
   ],
   "source": [
    "#  dim = 0 \n",
    "a = tf.constant(2.4)\n",
    "print(a.shape)\n",
    "print(len(a.shape))\n",
    "print(a)\n",
    "\n",
    "# dim = 1 \n",
    "a = tf.constant([2.4])\n",
    "print(a)\n",
    "print(\"dim = \",len(a.shape))\n",
    "\n",
    "# dim = 2\n",
    "b = tf.constant([[1,2],[3,4]])\n",
    "print(b)\n",
    "print(\"dim = \",len(b.shape))\n",
    "\n",
    "c = tf.constant([[[1,2],[3,4]]])\n",
    "print(c)\n",
    "print(\"dim = \",len(c.shape))\n",
    "\n",
    "d = tf.constant([[[1],[2]],[[3],[4]]])\n",
    "print(d)\n",
    "print(\"dim = \",len(d.shape))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "cell_style": "split"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([])\n",
      "0\n",
      "tensor(2.4000)\n",
      "tensor([2.4000])\n",
      "dim =  1\n",
      "tensor([[1, 2],\n",
      "        [3, 4]])\n",
      "dim =  2\n",
      "tensor([[[1, 2],\n",
      "         [3, 4]]])\n",
      "dim =  3\n",
      "tensor([[[1],\n",
      "         [2]],\n",
      "\n",
      "        [[3],\n",
      "         [4]]])\n",
      "dim =  3\n"
     ]
    }
   ],
   "source": [
    "#  dim = 0 \n",
    "a = torch.tensor(2.4)\n",
    "print(a.shape)\n",
    "print(len(a.shape))\n",
    "print(a)\n",
    "\n",
    "# dim = 1 \n",
    "a = torch.tensor([2.4])\n",
    "print(a)\n",
    "print(\"dim = \",len(a.shape))\n",
    "\n",
    "# dim = 2\n",
    "b = torch.tensor([[1,2],[3,4]])\n",
    "print(b)\n",
    "print(\"dim = \",len(b.shape))\n",
    "\n",
    "c = torch.tensor([[[1,2],[3,4]]])\n",
    "print(c)\n",
    "print(\"dim = \",len(c.shape))\n",
    "\n",
    "d = torch.tensor([[[1],[2]],[[3],[4]]])\n",
    "print(d)\n",
    "print(\"dim = \",len(d.shape))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* A tf.Variable represents a tensor whose value can be changed by running ops on it. Specific ops allow you to read and modify the values of this tensor.\n",
    "\n",
    "* A torch.Tensor can initialize tensor with certain shape. But this is not a recommded initialization methode"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "cell_style": "split",
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "()\n",
      "0\n",
      "<tf.Variable 'Variable:0' shape=() dtype=float32, numpy=2.4>\n",
      "<tf.Variable 'Variable:0' shape=(1,) dtype=float32, numpy=array([2.4], dtype=float32)>\n",
      "dim =  1\n",
      "<tf.Variable 'Variable:0' shape=(2, 2) dtype=int32, numpy=\n",
      "array([[1, 2],\n",
      "       [3, 4]], dtype=int32)>\n",
      "dim =  2\n",
      "<tf.Variable 'Variable:0' shape=(1, 2, 2) dtype=int32, numpy=\n",
      "array([[[1, 2],\n",
      "        [3, 4]]], dtype=int32)>\n",
      "dim =  3\n",
      "<tf.Variable 'Variable:0' shape=(2, 2, 1) dtype=int32, numpy=\n",
      "array([[[1],\n",
      "        [2]],\n",
      "\n",
      "       [[3],\n",
      "        [4]]], dtype=int32)>\n",
      "dim =  3\n"
     ]
    }
   ],
   "source": [
    "#  dim = 0 \n",
    "a = tf.Variable(2.4)\n",
    "print(a.shape)\n",
    "print(len(a.shape))\n",
    "print(a)\n",
    "\n",
    "# dim = 1 \n",
    "a = tf.Variable([2.4])\n",
    "print(a)\n",
    "print(\"dim = \",len(a.shape))\n",
    "\n",
    "# dim = 2\n",
    "b = tf.Variable([[1,2],[3,4]])\n",
    "print(b)\n",
    "print(\"dim = \",len(b.shape))\n",
    "\n",
    "c = tf.Variable([[[1,2],[3,4]]])\n",
    "print(c)\n",
    "print(\"dim = \",len(c.shape))\n",
    "\n",
    "d = tf.Variable([[[1],[2]],[[3],[4]]])\n",
    "print(d)SS\n",
    "print(\"dim = \",len(d.shape))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {
    "cell_style": "split"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([7.0065e-45])\n",
      "dim =  1\n",
      "tensor([[        0,         0,         0],\n",
      "        [        0, 919118944,         1]], dtype=torch.int32)\n",
      "dim =  2\n",
      "tensor([[[ 0.0000e+00, -2.5244e-29,  0.0000e+00, -2.5244e-29],\n",
      "         [ 1.6816e-44,  0.0000e+00,  6.0616e-06,  1.4013e-45],\n",
      "         [ 0.0000e+00,  0.0000e+00,  0.0000e+00,  0.0000e+00]],\n",
      "\n",
      "        [[ 5.9796e-06,  1.4013e-45,         nan,  0.0000e+00],\n",
      "         [ 1.4013e-45,  0.0000e+00,         nan,  2.2369e+08],\n",
      "         [ 6.0603e-06,  1.4013e-45,  1.4013e-45,  5.5101e-40]]])\n",
      "torch.Size([2, 3, 4])\n",
      "dim =  3\n",
      "tensor([[[ 2.3694e-38,  2.3694e-38,  2.3694e-38,  2.3511e-38],\n",
      "         [ 2.3511e-38,  2.3694e-38,  0.0000e+00,  0.0000e+00],\n",
      "         [ 0.0000e+00, -2.5244e-29,  1.7496e+18, -8.5920e+09]],\n",
      "\n",
      "        [[ 9.8091e-45,  0.0000e+00,  6.0616e-06,  1.4013e-45],\n",
      "         [ 0.0000e+00,  0.0000e+00,  0.0000e+00,  0.0000e+00],\n",
      "         [ 5.9796e-06,  1.4013e-45,         nan,  2.5250e-29]]])\n",
      "torch.Size([2, 3, 4])\n",
      "dim =  3\n"
     ]
    }
   ],
   "source": [
    "# dim = 1\n",
    "x = torch.Tensor(1)\n",
    "print(x)\n",
    "print('dim = ', len(x.shape))\n",
    "\n",
    "\n",
    "# dim = 2\n",
    "x = torch.IntTensor(2,3)\n",
    "print(x)\n",
    "print('dim = ', len(x.shape))\n",
    "\n",
    "# dim = 3\n",
    "x = torch.FloatTensor(2,3,4)\n",
    "print(x)\n",
    "print(x.shape)\n",
    "print('dim = ', len(x.shape))\n",
    "\n",
    "x = torch.empty(2,3,4)\n",
    "print(x)\n",
    "print(x.shape)\n",
    "print('dim = ', len(x.shape))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Variable 'Variable:0' shape=() dtype=int32, numpy=2>"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tf.Variable()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Tensor Initialize\n",
    "* From numpy or list\n",
    "* All one/zero/eye\n",
    "* Random normal/uniform"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {
    "cell_style": "split"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<tf.Variable 'Variable:0' shape=(3,) dtype=int64, numpy=array([1, 2, 3])>\n",
      "[1 2 3]\n",
      "tf.Tensor(\n",
      "[[1. 1. 1.]\n",
      " [1. 1. 1.]\n",
      " [1. 1. 1.]], shape=(3, 3), dtype=float32)\n",
      "tf.Tensor(\n",
      "[[[0. 0. 0.]\n",
      "  [0. 0. 0.]]], shape=(1, 2, 3), dtype=float32)\n",
      "tf.Tensor(\n",
      "[[1. 0. 0. 0.]\n",
      " [0. 1. 0. 0.]\n",
      " [0. 0. 1. 0.]\n",
      " [0. 0. 0. 1.]], shape=(4, 4), dtype=float32)\n",
      "tf.Tensor(\n",
      "[[-0.10190296 -0.643075    0.42584777]\n",
      " [-0.9554393   0.6987076  -0.3890078 ]\n",
      " [-0.06272173  0.40875173 -0.7274647 ]], shape=(3, 3), dtype=float32)\n",
      "tf.Tensor(\n",
      "[[-0.25065485  1.7940968   0.55680776]\n",
      " [ 0.6731767  -2.0355575   0.69362056]\n",
      " [ 0.56592643 -0.21413745  1.4197971 ]], shape=(3, 3), dtype=float32)\n"
     ]
    }
   ],
   "source": [
    "a = np.array([1,2,3])\n",
    "aa = tf.Variable(a)\n",
    "print(aa)\n",
    "\n",
    "# back to numpy\n",
    "print(aa.numpy())\n",
    "\n",
    "b = tf.ones(shape=[3,3])\n",
    "print(b)\n",
    "\n",
    "c = tf.zeros(shape=[1,2,3])\n",
    "print(c)\n",
    "\n",
    "d = tf.eye(4)\n",
    "print(d)\n",
    "\n",
    "e = tf.random.uniform([3,3],\n",
    "                     minval=-1,\n",
    "                     maxval= 1)\n",
    "print(e)\n",
    "\n",
    "f = tf.random.normal([3,3])\n",
    "print(f)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {
    "cell_style": "split"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([1, 2, 3])\n",
      "[1 2 3]\n",
      "tensor([[1., 1., 1.],\n",
      "        [1., 1., 1.],\n",
      "        [1., 1., 1.]])\n",
      "tensor([[[0., 0., 0.],\n",
      "         [0., 0., 0.]]])\n",
      "tensor([[1., 0., 0., 0.],\n",
      "        [0., 1., 0., 0.],\n",
      "        [0., 0., 1., 0.],\n",
      "        [0., 0., 0., 1.]])\n",
      "tensor([[ 0.9732,  0.5554, -0.2643],\n",
      "        [-0.2719,  0.4748, -0.9342],\n",
      "        [ 0.2863, -0.9117,  0.8118]])\n",
      "tensor([[ 0.3644, -0.3464,  0.3899],\n",
      "        [-0.7579, -0.4579, -0.7229],\n",
      "        [-0.1353, -0.1644, -1.0985]])\n",
      "tensor([[0.8672, 0.4170, 0.2993],\n",
      "        [0.2276, 0.8151, 0.8255],\n",
      "        [0.1173, 0.7587, 0.2998]])\n"
     ]
    }
   ],
   "source": [
    "a = np.array([1,2,3])\n",
    "aa = torch.from_numpy(a)\n",
    "print(aa)\n",
    "\n",
    "# back to numpy\n",
    "print(aa.numpy())\n",
    "\n",
    "b = torch.ones([3,3])\n",
    "print(b)\n",
    "\n",
    "c = torch.zeros([1,2,3])\n",
    "print(c)\n",
    "\n",
    "d = torch.eye(4)\n",
    "print(d)\n",
    "\n",
    "e = torch.rand([3,3]) *2 -1\n",
    "print(e)\n",
    "\n",
    "f = torch.empty(3,3)\n",
    "f = f.normal_()\n",
    "print(f)\n",
    "\n",
    "g = torch.rand_like(e)\n",
    "print(g)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "center"
   },
   "source": [
    "#### Indexing and slicing\n",
    "\n",
    "start : end: step (end is not included)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {
    "cell_style": "split"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tf.Tensor([0.30244088 0.48277724 0.66974723], shape=(3,), dtype=float32)\n",
      "tf.Tensor([0.30244088 0.48277724 0.66974723], shape=(3,), dtype=float32)\n",
      "tf.Tensor([0.30244088 0.48277724 0.66974723], shape=(3,), dtype=float32)\n",
      "(28,)\n",
      "(14, 14)\n",
      "(28, 28)\n"
     ]
    }
   ],
   "source": [
    "x = tf.random.uniform([2,28,28,3])\n",
    "\n",
    "print(x[0][0][0])\n",
    "print(x[0,0,0])\n",
    "print(x[0,0,0,:])\n",
    "\n",
    "print(x[0,:,0,0].shape)\n",
    "\n",
    "print(x[1,::2,::2,1].shape)\n",
    "\n",
    "print(x[0,...,0].shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {
    "cell_style": "split",
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([0.2286, 0.0121, 0.3937, 0.0671, 0.4020, 0.3109, 0.2263, 0.6636, 0.1600,\n",
      "        0.6500, 0.7802, 0.2829, 0.2400, 0.1953, 0.6289, 0.0833, 0.9539, 0.1139,\n",
      "        0.2049, 0.1019, 0.2217, 0.3020, 0.4510, 0.1544, 0.4524, 0.9684, 0.8236,\n",
      "        0.4322])\n",
      "tensor([0.2286, 0.0121, 0.3937, 0.0671, 0.4020, 0.3109, 0.2263, 0.6636, 0.1600,\n",
      "        0.6500, 0.7802, 0.2829, 0.2400, 0.1953, 0.6289, 0.0833, 0.9539, 0.1139,\n",
      "        0.2049, 0.1019, 0.2217, 0.3020, 0.4510, 0.1544, 0.4524, 0.9684, 0.8236,\n",
      "        0.4322])\n",
      "torch.Size([28])\n",
      "torch.Size([14, 14])\n",
      "torch.Size([3, 28])\n"
     ]
    }
   ],
   "source": [
    "x = torch.rand([2,3,28,28])\n",
    "print(x[0][0][0])\n",
    "print(x[0,0,0])\n",
    "print(x[0,0,0,:].shape)\n",
    "\n",
    "print(x[0,1,::2,::2].shape)\n",
    "\n",
    "print(x[0,...,0].shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Reshape\n",
    "\n",
    "Image shape: [b, w, h, c]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {
    "cell_style": "split"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(5, 784, 3)\n",
      "(5, 14, 56, 3)\n",
      "(5, 28, 28, 3)\n",
      "(1, 5, 28, 28, 3)\n",
      "(5, 28, 28, 3)\n",
      "(5, 28, 28, 3)\n"
     ]
    }
   ],
   "source": [
    "x = tf.random.uniform([5,28,28,3])\n",
    "\n",
    "print(tf.reshape(x, [5, 28*28, 3]).shape)\n",
    "print(tf.reshape(x, [5, 14, 56, 3]).shape)\n",
    "\n",
    "print(x.shape)\n",
    "\n",
    "print(tf.expand_dims(x, axis=0).shape)\n",
    "\n",
    "y = tf.expand_dims(x, axis=2)\n",
    "print(tf.squeeze(y, axis=2).shape)\n",
    "\n",
    "print(x.shape)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {
    "cell_style": "split"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([5, 3, 784])\n",
      "torch.Size([5, 3, 14, 56])\n",
      "torch.Size([5, 3, 28, 28])\n"
     ]
    }
   ],
   "source": [
    "x = torch.rand([5,3,28,28])\n",
    "\n",
    "print(x.view(5,3,28*28).shape)\n",
    "print(x.view(5,3,14,56).shape)\n",
    "\n",
    "print(x.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.7.5 64-bit ('tf2': conda)",
   "language": "python",
   "name": "python37564bittf2conda8449d89771174c2f89eb8324501cb93e"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
